﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GreatB2C.Config.SiteInfo;

namespace GreatB2C.Web.Manage.SystemSet.Shop
{
    public partial class ShopConfig : Inherit.ManagePage
    {
        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                regMyPosition.DataInitial( );
                regDefaultAddress.DataInitial( );

                LoadSetting( );
            }
        }

        protected void LoadSetting( )
        {
            radlEnableGuestBuy.SelectedValue = Config.Shop.ShopConfig.EnableGuestBuy.ToString( );
            radlTaxRateTypeSet.SelectedValue = Config.Shop.ShopConfig.TaxRateTypeSet.ToString( );
            txtTaxRate.Text = Config.Shop.ShopConfig.TaxRate.ToString( );
            txtPrefixOrderFormNum.Text = Config.Shop.ShopConfig.PrefixOrderFormNum.ToString( );
            txtPrefixPaymentNum.Text = Config.Shop.ShopConfig.PrefixPaymentNum.ToString( );
            txtShopServicesKey.Text = Config.Shop.ShopConfig.ShopServicesKey.ToString( );
            string[] pos = Config.Shop.ShopConfig.MyPosition.ToString( ).Split( ',' );
            if ( pos.Length == 3 )
            {
                regMyPosition.Province = pos[ 0 ];
                regMyPosition.City = pos[ 1 ];
                regMyPosition.Area = pos[ 2 ];
            }

            string[] defAddress = Config.Shop.ShopConfig.DefaultAddress.ToString( ).Split( ',' );
            if ( defAddress.Length == 3 )
            {
                regDefaultAddress.Province = pos[ 0 ];
                regDefaultAddress.City = pos[ 1 ];
                regDefaultAddress.Area = pos[ 2 ];
            }
            txtPostCode.Text = Config.Shop.ShopConfig.PostCode.ToString( );

            string EnablePartPay = Config.Shop.ShopConfig.NeedLogin.ToString( );
            if ( EnablePartPay == "1" )
                radlEnablePartPay1.Checked = true;
            else if ( EnablePartPay == "0" )
                radlEnablePartPay0.Checked = true;
            txtAge.Text = Config.Shop.ShopConfig.Age.ToString( );
            txtOrderProductNumber.Text = Config.Shop.ShopConfig.OrderProductNumber.ToString( );
            txtMoneyPresentPoint.Text = Config.Shop.ShopConfig.MoneyPresentPoint.ToString( );
            radlPayPassword.SelectedValue = Config.Shop.ShopConfig.PayPassword.ToString( );
            radlEnableCoupon.SelectedValue = Config.Shop.ShopConfig.EnableCoupon.ToString( );
            radlSetFunctionary.SelectedValue = Config.Shop.ShopConfig.SetFunctionary.ToString( );
            radlThumb.SelectedValue = Config.Shop.ShopConfig.Thumb.ToString( );
            radlWatermark.SelectedValue = Config.Shop.ShopConfig.Watermark.ToString( );
            radlGwcProductType.SelectedValue = Config.Shop.ShopConfig.GwcProductType.ToString( );
            radlPaymentProductType.SelectedValue = Config.Shop.ShopConfig.PaymentProductType.ToString( );
            radlPreviewProductType.SelectedValue = Config.Shop.ShopConfig.PreviewProductType.ToString( );
            radlGwcSaleType.SelectedValue = Config.Shop.ShopConfig.GwcSaleType.ToString( );
            radlPaymentSaleType.SelectedValue = Config.Shop.ShopConfig.PaymentSaleType.ToString( );
            radlPreviewSaleType.SelectedValue = Config.Shop.ShopConfig.PreviewSaleType.ToString( );
            radlGwcMarkPrice.SelectedValue = Config.Shop.ShopConfig.GwcMarkPrice.ToString( );
            radlPaymentMarkPrice.SelectedValue = Config.Shop.ShopConfig.PaymentMarkPrice.ToString( );
            radlPreviewMarkPrice.SelectedValue = Config.Shop.ShopConfig.PreviewMarkPrice.ToString( );
            radlAllowDownloadOvertime.SelectedValue = Config.Shop.ShopConfig.AllowDownloadOvertime.ToString( );
            radlOrderDetailsTotal.SelectedValue = Config.Shop.ShopConfig.OrderDetailsTotal.ToString( );
            radlDiscountBasePriceComputeType.SelectedValue = Config.Shop.ShopConfig.DiscountBasePriceComputeType.ToString( );
            radlAgentPaymentComputType.SelectedValue = Config.Shop.ShopConfig.AgentPaymentComputType.ToString( );
            radlAgentPaymentDeductMoneyType.SelectedValue = Config.Shop.ShopConfig.AgentPaymentDeductMoneyType.ToString( );
            radlGwcProducdtThumb.SelectedValue = Config.Shop.ShopConfig.GwcProducdtThumb.ToString( );
            string[] GwcProductThumbSize = Config.Shop.ShopConfig.GwcProductThumbSize.ToString( ).Split( ',' );
            if ( GwcProductThumbSize.Length == 2 )
            {
                txtGwcProductThumbWidth.Text = GwcProductThumbSize[ 0 ];
                txtGwcProductThumbHeight.Text = GwcProductThumbSize[ 1 ];
            }
            radlPaymentProductThumb.SelectedValue = Config.Shop.ShopConfig.PaymentProductThumb.ToString( );
            string[ ] PaymentProductThumbSize = Config.Shop.ShopConfig.PaymentProductThumbSize.ToString( ).Split( ',' );
            if ( PaymentProductThumbSize.Length == 2 )
            {
                txtPaymentProductThumbWidth.Text = GwcProductThumbSize[ 0 ];
                txtPaymentProductThumbHeight.Text = GwcProductThumbSize[ 1 ];
            }
            radlPreviewProductThumb.SelectedValue = Config.Shop.ShopConfig.PreviewProductThumb.ToString( );
            string[] PreviewProductThumbSize = Config.Shop.ShopConfig.PaymentProductThumbSize.ToString( ).Split( ',' );
            if ( PreviewProductThumbSize.Length == 2 )
            {
                txtPreviewProductThumbWidth.Text = GwcProductThumbSize[ 0 ];
                txtPreviewProductThumbHeight.Text = GwcProductThumbSize[ 1 ];
            }
            radlShowOrderProductListThumb.SelectedValue = Config.Shop.ShopConfig.ShowOrderProductListThumb.ToString( );
            string[] OrderProductListThumbSize = Config.Shop.ShopConfig.OrderProductListThumbSize.ToString( ).Split( ',' );
            if ( OrderProductListThumbSize.Length == 2 )
            {
                txtOrderProductListThumbWidth.Text = GwcProductThumbSize[ 0 ];
                txtOrderProductListThumbHeight.Text = GwcProductThumbSize[ 1 ];
            }
            radlShowProducdtListThumb.SelectedValue = Config.Shop.ShopConfig.ShowProducdtListThumb.ToString( );
            string[] ProductListThumbSize = Config.Shop.ShopConfig.ProductListThumbSize.ToString( ).Split( ',' );
            if ( ProductListThumbSize.Length == 2 )
            {
                txtProductListThumbWidth.Text = GwcProductThumbSize[ 0 ];
                txtProductListThumbHeight.Text = GwcProductThumbSize[ 1 ];
            }
            txtContinueBuy.Text = Config.Shop.ShopConfig.ContinueBuy.ToString( );
            txtAgainBuy.Text = Config.Shop.ShopConfig.AgainBuy.ToString( );
            string ShoppingFlow = Config.Shop.ShopConfig.ShoppingFlow.ToString( );
            if ( ShoppingFlow == "1" )
                radNewShoppingFlow.Checked = true;
            else if ( ShoppingFlow == "2" )
                radOldShoppingFlow.Checked = true;

            if ( Config.Shop.ShopConfig.ShoppingCartSendPresent.Trim( ) != "" )
                chkShoppingCartSendPresent.Checked = Convert.ToBoolean( Config.Shop.ShopConfig.ShoppingCartSendPresent.ToString( ) );
            if ( Config.Shop.ShopConfig.CardOnlinePayMessage.Trim( ) != "" )
                chkCardOnlinePayMessage.Checked = Convert.ToBoolean( Config.Shop.ShopConfig.CardOnlinePayMessage.ToString( ) );
            if ( Config.Shop.ShopConfig.CardOnlinePayMobilePhone.Trim( ) != "" )
                chkCardOnlinePayMobilePhone.Checked = Convert.ToBoolean( Config.Shop.ShopConfig.CardOnlinePayMobilePhone.ToString( ) );
            if ( Config.Shop.ShopConfig.CardOnlinePayMail.Trim( ) != "" )
                chkCardOnlinePayMail.Checked = Convert.ToBoolean( Config.Shop.ShopConfig.CardOnlinePayMail.ToString( ) );
            if ( Config.Shop.ShopConfig.CouponDeliverPay.Trim( ) != "" )
                chkCouponDeliverPay.Checked = Convert.ToBoolean( Config.Shop.ShopConfig.CouponDeliverPay.ToString( ) );
        }

        protected void BtnSubmit_Click( object sender , EventArgs e )
        {
            string myPosition = regMyPosition.Province + "," + regMyPosition.City + "," + regMyPosition.Area;
            string defAddress = regDefaultAddress.Province + "," + regDefaultAddress.City + "," + regDefaultAddress.Area;
            string EnablePartPay = "0";
            if ( radlEnablePartPay1.Checked )
                EnablePartPay = "1";
            else if ( radlEnablePartPay0.Checked )
                EnablePartPay = "0";

            string GwcProductThumbSize = txtGwcProductThumbWidth.Text.Trim( ) + "," + txtGwcProductThumbHeight.Text.Trim( );
            string PaymentProductThumbSize = txtPaymentProductThumbWidth.Text.Trim( ) + "," + txtPaymentProductThumbHeight.Text.Trim( );
            string PreviewProductThumbSize = txtPreviewProductThumbWidth.Text.Trim( ) + "," + txtPreviewProductThumbHeight.Text.Trim( );
            string OrderProductListThumbSize = txtOrderProductListThumbWidth.Text.Trim( ) + "," + txtOrderProductListThumbHeight.Text.Trim( );
            string ProductListThumbSize = txtProductListThumbWidth.Text.Trim( ) + "," + txtProductListThumbHeight.Text.Trim( );

            string ShoppingFlow = "1";
            if ( radNewShoppingFlow.Checked )
                ShoppingFlow = "1";
            else if ( radOldShoppingFlow.Checked )
                ShoppingFlow = "2";

            bool isSuccess = Config.Shop.ShopConfig.Config( radlEnableGuestBuy.SelectedValue.ToString( ) , radlTaxRateTypeSet.SelectedValue.ToString( ) , txtTaxRate.Text.Trim( ) , txtPrefixOrderFormNum.Text.Trim( )
            , txtPrefixPaymentNum.Text.Trim( ) , txtShopServicesKey.Text.Trim( ) , myPosition , defAddress
            , txtPostCode.Text.Trim( ) , EnablePartPay , txtAge.Text.Trim( ) , txtOrderProductNumber.Text.Trim( )
            , txtMoneyPresentPoint.Text.Trim( ) , radlPayPassword.SelectedValue.Trim( ) , radlEnableCoupon.SelectedValue.Trim( ) , radlSetFunctionary.SelectedValue.Trim( )
            , radlThumb.SelectedValue.Trim( ) , radlWatermark.SelectedValue.Trim( ) , radlGwcProductType.SelectedValue.Trim( ) , radlPaymentProductType.SelectedValue.Trim( )
            , radlPreviewProductType.SelectedValue.Trim( ) , radlGwcSaleType.SelectedValue.Trim( ) , radlPaymentSaleType.SelectedValue.Trim( ) , radlPreviewSaleType.SelectedValue.Trim( )
            , radlGwcMarkPrice.SelectedValue.Trim( ) , radlPaymentMarkPrice.SelectedValue.Trim( ) , radlPreviewMarkPrice.SelectedValue.Trim( ) , radlAllowDownloadOvertime.SelectedValue.Trim( )
            , radlOrderDetailsTotal.SelectedValue.Trim( ) , radlDiscountBasePriceComputeType.SelectedValue.Trim( ) , radlAgentPaymentComputType.SelectedValue.Trim( ) , radlAgentPaymentDeductMoneyType.SelectedValue.Trim( )
            , radlGwcProducdtThumb.SelectedValue.Trim( ) , GwcProductThumbSize , radlPaymentProductThumb.SelectedValue.Trim( ) , PaymentProductThumbSize
            , radlPreviewProductThumb.SelectedValue.Trim( ) , PreviewProductThumbSize , radlShowOrderProductListThumb.Text.Trim( ) , OrderProductListThumbSize
            , radlShowProducdtListThumb.SelectedValue.Trim( ) , ProductListThumbSize , txtContinueBuy.Text.Trim( ) , txtAgainBuy.Text.Trim( )
            , ShoppingFlow , chkShoppingCartSendPresent.Checked.ToString( ) , chkCardOnlinePayMessage.Checked.ToString( ) , chkCardOnlinePayMobilePhone.Checked.ToString( )
            , chkCardOnlinePayMail.Checked.ToString( ) , chkCouponDeliverPay.Checked.ToString( ) );

            if ( isSuccess )
            {
                Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "商店参数配置成功！" ) + "&rtn=SystemSet/Shop/ShopConfig.aspx" );
            }
            else
            {
                Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "商店参数配置失败，请联系管理员！" ) + "&rtn=SystemSet/Shop/ShopConfig.aspx" );
            }
        }

        public string getPointName( )
        {
            if ( UserConfig.PointName.Trim( ) != "" )
                return UserConfig.PointName;
            else
                return "点券";
        }

        public string getPointUnit( )
        {
            if ( UserConfig.PointUnit.Trim( ) != "" )
                return UserConfig.PointUnit;
            else
                return "点";
        }
    }
}
